package org.example.backend.util;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@ComponentScan("org.example.backend.util")
public class TokenTest {
    public static void main(String[] args) {
        // 创建Spring容器
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(TokenTest.class);

        // 获取TokenTestUtil实例
        TokenUtil tokenUtil = context.getBean(TokenUtil.class);

        // 生成简单令牌
        String username = "testuser";
        String token = tokenUtil.generateToken(username);
        System.out.println("生成的令牌: " + token);

        // 生成带额外信息的令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("role", "admin");
        claims.put("userId", 1001);
        String tokenWithClaims = tokenUtil.generateToken(claims, username);
        System.out.println("带额外信息的令牌: " + tokenWithClaims);

        // 验证令牌
        boolean isValid = tokenUtil.validateToken(token, username);
        System.out.println("令牌是否有效: " + isValid);

        // 从令牌中提取信息
        String extractedUsername = tokenUtil.extractUsername(token);
        System.out.println("从令牌中提取的用户名: " + extractedUsername);

        // 关闭容器
        context.close();
    }
}